<?php
include "../lib/DAL/DmLoaiMonHoc.php";

class LoaiMonhocBLL{
    function __construct(){
    }
    
    function __destruct(){
    }

    public function setLoaiMonhocBLL($row)
    {
        $loai_mh= new DmLoaiMonHoc();
        $loai_mh->setLoaiMonhoc($row['madm_lmh'],$row['ten_lmh']);
        return $loai_mh;   
    }
    
    public function getListLoaiMonhoc(){
        $ex=mysql_query("SELECT * FROM dm_loaimonhoc");
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $ds[]=$this->setLoaiMonhocBLL($row);
            }
        }
        return $ds;
    }
    
    public function getItemLoaiMonhoc($madm_lmh){
    $sql = "select * from dm_loaimonhoc where madm_lmh='".$madm_lmh."'";
    $ex = mysql_query($sql);
        mysql_num_rows($ex);
        $kq = mysql_fetch_array($ex);
        return $kq;
    }
    public function insertLMH($lmh){
        $sql ="INSERT INTO dm_loaimonhoc(madm_lmh,ten_lmh) VALUES (
            '".$lmh->getmadm_lmh()."'
            ,'".$lmh->getten_lmh()."' 
        )";
        $result = mysql_query($sql) or die (mysql_error());
       return $result;
    }
    
    public function updateLMH($lmh){
        $sql = "UPDATE dm_loaimonhoc SET ten_lmh = '".$lmh->getten_lmh()."' WHERE madm_lmh = '".$lmh->getmadm_lmh()."'";
          $result = mysql_query($sql) or die (mysql_error());
       return $result;
    }
    
    public function deleteLMH($madm_lmh){
        $sql="delete from dm_loaimonhoc where madm_lmh = '".$madm_lmh."'";
         $result = mysql_query($sql) or die (mysql_error());
       return $result;
    }
    
    public function deleteAll(){
        $sql="DELETE FROM dm_loaimonhoc";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
	function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" madm_lmh LIKE '%$kw%' OR ten_lmh LIKE '%$kw%'";			
			$dieukien .= $qse;	
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
		
	}
    function timkiem($where){
        $sql="SELECT madm_lmh, ten_lmh FROM dm_loaimonhoc $where";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->setLoaiMonhocBLL($row);
            }
        }
        return $dstk;
    }
    public function getPages($where,$offset,$limit){
        $result = mysql_query(" SELECT madm_lmh, ten_lmh FROM dm_loaimonhoc $where LIMIT $offset, $limit");
        $arr_lmh= array();
        
        while($row = mysql_fetch_array($result)){
            $arr_lmh[] =  $this->setLoaiMonhocBLL($row);
        }
        return $arr_lmh;
    }
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='';
		for($i=1;$i<=$totalPage;$i++){
            $pager.="<a href='$url&p=$i' class='$css'>$i</a>";
		}
		return $pager;
	}
}
?>